package com.bob.web.controller.system;

import com.bob.web.common.constant.CodeConstant;
import com.bob.web.common.constant.Page;
import com.bob.web.common.exception.ServiceException;
import com.bob.web.controller.BaseController;
import com.bob.web.entity.system.Admin;
import com.bob.web.service.system.AdminService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * 名称: AdminController<br>
 * 描述:<br>
 * 类型: JAVA<br>
 * 最近修改时间:2017/7/20 11:18<br>
 *
 * @author BoYangsh
 * @version [版本号, V1.0]
 * @since 2017/7/20 11:18
 */
@RestController
@RequestMapping("admin")
public class AdminController extends BaseController{

	@Autowired
	private AdminService adminService;

	/**
	 * 登录
	 * @param username
	 * @param password
	 * @param rememberMe
	 */
	@RequestMapping("login")
	public Admin login(String username, String password, Boolean rememberMe) {
		Subject subject = SecurityUtils.getSubject();
		//判断是否已经的登录,已经登录就直接返回
		if(subject.isAuthenticated()) {
			return getCurrentUser();
		}
		UsernamePasswordToken token = new UsernamePasswordToken(username, password, rememberMe == null ? false : true);
		try{
			subject.login(token);
			return getCurrentUser();
		} catch ( UnknownAccountException uae ) { //用户不存在
			throw new ServiceException(CodeConstant.USER_NOT_EXISTED);
		} catch ( IncorrectCredentialsException ice ) { //密码错误
			throw new ServiceException(CodeConstant.PASSWORD_IS_INCORRECT);
		} catch ( LockedAccountException lae ) { //账号已经锁定
			throw new ServiceException(CodeConstant.ACCOUNT_IS_LOCKED);
		} catch ( ExcessiveAttemptsException eae ) {  //错误登录次数过多（这个在凭证策略里配置的）
			throw new ServiceException(CodeConstant.LOGIN_MORE_TIMES);
		} catch ( AuthenticationException ae ) { //登录位置异常
			throw new ServiceException(CodeConstant.LOGIN_ERROR);
		}
	}

	/**
	 * 登出
	 */
	@RequestMapping("logout")
	public void logout() {
		Subject subject = SecurityUtils.getSubject();
		subject.logout();
	}

	/**
	 * 获得用户角色
	 * @return
	 */
	@RequestMapping("info")
	public Admin info() {
		String username = (String) SecurityUtils.getSubject().getPrincipal();
		return adminService.findAdminByName(username);
	}

	@RequestMapping("find_page")
	public Page<Admin> findPage(Page<Admin> page) {
		return adminService.findPage(page);
	}

	@RequestMapping("add")
	public void add(Admin user) {
		adminService.add(user);
	}

	@RequestMapping("delete")
	public void delete(String id) {
		adminService.delete(id);
	}

	@RequestMapping("update")
	public void update(Admin user) {
		adminService.update(user);
	}

	@RequestMapping("detail")
	public Admin findUserById(Long id) {
		Admin user = adminService.findUserById(id);
		if(user == null) { //用户不存在
			throw new ServiceException(CodeConstant.USER_NOT_EXISTED);
		}
		return user;
	}

//	/**
//	 * 未登录
//	 */
//	@RequestMapping("not_login")
//	public void notLogin() {
//		throw new ServiceException(CodeConstant.NO_LOGIN);
//	}
}
